<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>投票活动列表</title>
    <#include "/common/link.ftl">

</head>
<body class="hold-transition skin-black sidebar-mini">
<div class="wrapper">
    <#include "/common/navbar.ftl">

    <#assign currentMenu="activity"/>

    <#include "/common/menu.ftl">
    <div class="content-wrapper">
        <section class="content-header">
            <h1>投票活动列表</h1>
        </section>
        <section class="content">
            <div class="box">
                <!--高级查询--->
                <form class="form-inline" id="searchForm" action="/activity/list" method="post"  style="margin-top: 20px">
                    <input type="hidden" name="currentPage" id="currentPage" value="1">
                    <#if EMPLOYEE_IN_SESSION.admin>
                        <a href="javascript:void(0);" class="btn btn-success btn-input btn-insert" style="margin: 10px">
                            <span class="glyphicon glyphicon-plus"></span> 添加
                        </a>
                    </#if>
                </form>
                <!--编写内容-->
                <div class="box-body table-responsive">
                    <table class="table table-hover table-bordered table-striped">
                        <thead>
                        <tr>
                            <th>编号</th>
                            <th>投票主题</th>
                            <th>投票类型</th>
                            <th>投票状态</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        <#list pageInfo.list as a>
                            <tr>
                                <td>${a.id}</td>
                                <td>${a.title}</td>
                                <td>${a.typeName}</td>
                                <td><span style="color:${(a.status?string('green', 'orange'))!}">${(a.status?string("投票中", "停止投票"))!}</span></td>
                                <td>
                                    <a href="#" class="btn btn-info btn-xs btn-input btn-checkin" data-id="${a.id}">
                                        <span class="glyphicon glyphicon-arrow-right"></span> 投票</a>

                                    <a href="#" class="btn btn-primary btn-xs btn-see" data-id="${a.id}">
                                        <span class="glyphicon glyphicon-phone-alt"></span> 查看</a>
                                    <#if EMPLOYEE_IN_SESSION.admin>
                                    <#if a.status>
                                        <a href="#" class="btn btn-xs btn-warning btn-status" data-json='{"id":${a.id}, "status":${a.status}}'>
                                            <span class="glyphicon glyphicon-remove"></span> 停止</a>
                                    <#else>
                                        <a href="#" class="btn btn-success btn-xs btn-status" data-json='{"id":${a.id}, "status":${(a.status?string("1", "0"))!0}}'>
                                            <span class="glyphicon glyphicon-ok"></span> 开始</a>
                                    </#if>
                                    <a class="btn btn-danger btn-xs btn-delete" data-url="/activity/delete?id=${a.id}">
                                        <span class="glyphicon glyphicon-trash"></span> 删除</a>
                                    </#if>
                                </td>
                            </tr>
                        </#list>
                        </tbody>
                    </table>
                    <#include "/common/page.ftl" >
                </div>
            </div>
        </section>
    </div>
    <#include "/common/footer.ftl" >
</div>
<div id="editModal" class="modal fade" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <form class="form-horizontal" id="insertFrom" action="/activity/saveActivity" method="post">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title">添加投票活动</h4>
                </div>
                <div class="modal-body">
                    <input type="hidden" name="id">
                    <div class="form-group">
                        <label for="inputEmail3" class="col-sm-2 control-label">投票标题</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" name="title" placeholder="请输入投票标题">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="inputEmail3" class="col-sm-2 control-label">投票选项</label>
                        <div class="col-sm-10" id="items">
                            <input type="text" class="form-control" name="itemArr" placeholder="请输入投票选项,为空则该选项弃用" style="margin-top: 10px;">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="inputPassword3" class="col-sm-2 control-label">投票类型</label>
                        <div class="col-sm-3">
                            <label class="radio-inline">
                                <input type="radio" name="type" value="0" checked> 单选
                            </label>
                            <label class="radio-inline">
                                <input type="radio" name="type" value="1"> 多选
                            </label>
                        </div>
                        <label for="inputPassword3" class="col-sm-2 control-label">投票状态</label>
                        <div class="col-sm-4">
                            <label class="radio-inline">
                                <input type="radio" name="status" value="0" checked> 暂不发起
                            </label>
                            <label class="radio-inline">
                                <input type="radio" name="status" value="1"> 立即发起
                            </label>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-warning btn-insertItem"><span class="glyphicon glyphicon-plus"></span> 增加选项</button>
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary btn-saveActivity">确定</button>
                </div>
            </form>
        </div>
    </div>
</div>
<script>
    $(function () {
        $(".btn-checkin").click(function () {
            var id = $(this).data("id");
            $.post("/activity/checkin?id=" + id, function (data) {
                if(data.success){
                    window.location.href = "/activity/input?id=" + id
                }else{
                    Swal.fire({
                        title : data.msg,
                        icon : "warning"
                    })
                }
            });
        });

        $(".btn-see").click(function () {
            var id = $(this).data("id");
            $.post("/activity/seeActivity?id=" + id, function (data) {
                if(data.success){
                    window.location.href = "/activity/show?id=" + id
                }else{
                    Swal.fire({
                        title : data.msg,
                        icon : "warning"
                    })
                }
            });
        });


        $(".btn-insert").click(function () {
            $("#editModal").modal("show");
        });

        $(".btn-insertItem").click(function () {
            var str = '<input type="text" class="form-control" name="itemArr" placeholder="请输入投票选项,为空则该选项弃用" style="margin-top: 10px;">';
            $("#items").append(str);
        });

        $(".btn-saveActivity").click(function () {
            Swal.fire({
                title : "确定添加该投票活动吗?",
                icon : "info",
                showCancelButton: true,
                cancelButtonText :"取消",
                confirmButtonText: "确定"
            }).then((result)=>{
                if(result.value){
                    $("#insertFrom").ajaxSubmit(function (data) {
                        if(data.success) window.location.reload();
                    });
                }
            });
        });

        $(".btn-status").click(function () {
            var json = $(this).data("json");
            console.log(json);
            Swal.fire({
                title : json.status ? "确定结束投票吗?" : "确定开启投票吗?",
                icon : "info",
                showCancelButton: true,
                cancelButtonText :"取消",
                confirmButtonText: "确定"
            }).then((result)=>{
                if(result.value){
                    $.post("/activity/updateStatus", "id=" + json.id, function (data) {
                        if(data.success){
                            Swal.fire({
                                title : "操作成功!",
                                icon : "success",
                            });
                            window.location.reload();
                        }
                    })
                }
            });
        });
    });
</script>
</body>
</html>
